package com.party.orders.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.party.domain.Coupon;
import com.party.dto.Page;
import com.party.exception.ExceptionMsgEnum;
import com.party.exception.ServiceException;
import com.party.orders.dto.CountAmountDTO;
import com.party.orders.mapper.CouponMapper;
import com.party.orders.mapper.CouponUserMapper;
import com.party.orders.service.CouponService;
import com.party.orders.vo.CouponVO;
/**
 * 
 * @author Caizhf
 * @date 2017年7月29日下午3:23:49
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: </p>
 *
 */
@Service
public class CouponServiceImpl implements CouponService{

	@Autowired
	private CouponMapper couponMapper;
	@Autowired
	private CouponUserMapper couponUserMapper;
	

	@Override
	public Coupon findById(int cpid) {
		Coupon cp = couponMapper.findById(cpid);
		return cp;
	}

	
	@Override
	@Transactional(readOnly=true)
	public List<CouponVO> list(int pageNum, int pageSize, String key, Integer userId, Boolean isOverdue, Integer couponStatus) {
		int totleRecord = 0;
		Page page = null;
		List<CouponVO> list=null;
		//如果需要查过期的劵
		if(isOverdue!=null){
			totleRecord = couponMapper.countList(key,userId,new Date(System.currentTimeMillis()),isOverdue, couponStatus);
			page = new Page(totleRecord,pageNum,pageSize);
			list = couponMapper.list(key, userId, page.getStartindex(), pageSize,new Date(System.currentTimeMillis()), isOverdue, couponStatus);
		}
		//如果不需要查过期的劵
		else{
			totleRecord = couponMapper.countList(key,userId,null,null,couponStatus);
			page = new Page(totleRecord,pageNum,pageSize);
			list = couponMapper.list(key, userId, page.getStartindex(), pageSize, null, null,couponStatus);
		}
		return list;
	}


	@Override
	@Transactional
	public void save(Integer userId, int cpid) {
		Coupon coupon = couponMapper.findById(cpid);
		//这张优惠券是否存在
		if(coupon==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_DB_NOTEXIT);
		int number = coupon.getcNumber();
		//判断数量
		if(number<=0)
			throw new ServiceException("该优惠券已经领完了！");
		//判断是否已经领过了
		if(couponUserMapper.findStatusByUserIdAndCpid(userId,cpid)!=null)
			throw new ServiceException("这张优惠券您已经领过了，赶紧去使用吧!");
		//更新劵拥有的数量
		couponMapper.updatecNumberById(cpid,--number);
		//如果前面都通过了，就可以插入新记录并且关联了了
		if(couponUserMapper.save(userId,cpid,UNUSE)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_ERROR_MSG);
	}


	
	@Override
	public List<Coupon> listByCanUse(CountAmountDTO amountDTO, int userId) {
		List<Coupon> results = new ArrayList<Coupon>();
		//查出该用户所拥有的所有状态为0（未使用）,未过期的优惠券,不分页
		List<CouponVO> coupons = couponMapper.list(null, userId, null, null, new Date(System.currentTimeMillis()), false, UNUSE);
		//对优惠券进行判断可用性
		if(coupons!=null&&coupons.size()!=0)
		for(Coupon c : coupons){
			//满减所需金额
			double couponAmountNeed = c.getAmountNeed();
			//优惠产品类型
			int productType = c.getProductType();
			//判断选购的产品（0场地，1布置商品，2达人服务，3包车服务，4餐饮服务，5套餐）（注意数量）价格是否达到优惠券的满减条件
			if((productType==0&&(couponAmountNeed<=amountDTO.getSiteTotalAmount()))
					||(productType==1&&(couponAmountNeed<=amountDTO.getDecorationProductTotalAmount()))
					||(productType==2&&(couponAmountNeed<=amountDTO.getTalentServiceTotalAmount()))
					||(productType==3&&(couponAmountNeed<=amountDTO.getBusTotalAmount()))
					||(productType==4&&(couponAmountNeed<=amountDTO.getCaterTotalAmount()))
					||(productType==5&&(couponAmountNeed<=amountDTO.getSetMealTotalAmount()))
					//排除套餐，看看能不能用总价劵
					||(productType==6&&(couponAmountNeed<=amountDTO.countAllProduct()))
						){
				//只要满足了满减条件，将优惠券直接加入结果集
				results.add(c);
			}
		}
		//返回可用结果集
		return results;
	}

}
